<?php

/*
 * Snippet 1 from
 * http://www.php.net/manual/en/language.types.boolean.php
 */


/*
 * Create an instance of IndentedTextWriter to hold the generated code
 */

$writer = new IndentedTextWriter();

/*
 * Create an instance of the code generator options class
 */

$options = new CodeGeneratorOptions();

/*
 * Set the bracing style:
 */

$options->set_BracingStyle(BracingStyle::KR);

/*
 * both $writer and $options will be arguments to one of
 * the PHPCodeProvider code generating methods.
 * Create an instance of the PHP code provider class
 */

$provider = new PHPCodeProvider();

/*
 * Start creating the code tree.
 */

$defaultNamespace = new CodeNamespace();

/*
 * In the Manual exemple $show_separators appears twice
 * so create a variable for it
 */

$show_separators_ref = new VariableReferenceExpression('show_separators');

/*
 * Start adding statements to the namespace.
 * The first if statement
 */

$ifStatement1 = new IfElseStatement(
    new BinaryOperatorExpression(
        BinaryOperator::VALUE_EQUALITY, 
        new VariableReferenceExpression('version'), 
        new PrimitiveExpression('show version')));

$echo1 = new EchoStatement();
$echo1->OutputParameters->add(
    new PrimitiveExpression('The version is 1.23'));

$ifStatement1->TrueStatements->add($echo1);

$comment1 = new CommentStatement();
$comment1->Comment = new Comment(
    "== is an operator which tests " . PHP_EOL . 
    "equality and returns a boolean");

$ifStatement1->Comments->add($comment1);

/*
 * The second if statement
 */

$ifStatement2 = new IfElseStatement(
    new BinaryOperatorExpression(
        BinaryOperator::VALUE_EQUALITY,
        $show_separators_ref,
        new ConstantReferenceExpression('TRUE')));

$echo2 = new EchoStatement();
$echo2->OutputParameters->add(
    new PrimitiveExpression('<hr>\n'));

$ifStatement2->TrueStatements->add($echo2);

$comment2 = new CommentStatement(new Comment(
    'this is not necessary..'));

$ifStatement2->Comments->add($comment2);

/*
 * The last if statement
 */

$ifStatement3 = new IfElseStatement($show_separators_ref);
$ifStatement3->TrueStatements->add($echo2);

$comment3 = new CommentStatement(new Comment(
    '...because this can be used with exactly the same meaning:'));
$ifStatement3->Comments->add($comment3);

/*
 * Add the if statements to the default namespace
 */

$defaultNamespace->Statements->add($ifStatement1);
$defaultNamespace->Statements->add($ifStatement2);
$defaultNamespace->Statements->add($ifStatement3);

/*
 * Make some space
 */

foreach($defaultNamespace->Statements as $statement)
{
    $statement->BlankLinesAfter = 1;
}

/*
 * Generate the code
 */

$provider->generateCodeFromNamespace($defaultNamespace, $writer, $options);

/*
 * Print the generated code
 */

echo 
    '<pre>' . str_replace(
        '<br />', '', highlight_string($writer->get_String(), true)) . 
    '<pre/>';


?>
